package kr.co.valueworks.fishing.activity;

import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.Toast;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.OverlayItem;
import java.util.ArrayList;
import java.util.List;

import kr.co.valueworks.fishing.R;
import kr.co.valueworks.fishing.R.drawable;
import kr.co.valueworks.fishing.R.id;
import kr.co.valueworks.fishing.R.layout;
 
public class FishingMap2 extends MapActivity {
	private MapView map = null;
	private MyLocationOverlay me = null;
	private ArrayList<GeoPoint> gp = new ArrayList<GeoPoint>(); 
	int cnt = 0;
	final int INITIAL_ZOOM_LEVEL = 16;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fishing_map2);
		
		initGeoPoint();
	
		// 맵뷰에 줌콘트롤러 추가
		map = (MapView)findViewById(R.id.map); 
		map.getController().setCenter(gp.get(0));
		map.getController().setZoom(INITIAL_ZOOM_LEVEL);
		ViewGroup zoom = (ViewGroup)findViewById(R.id.zoom);
		zoom.addView(map.getZoomControls()); 
				 
		//마커
		Drawable marker = getResources().getDrawable(R.drawable.pin_spot2);
		marker.setBounds(0, 0, marker.getIntrinsicWidth(), marker.getIntrinsicHeight());
		
		/*ItemizedOverlay를 상속받은 SitesOverlay(marker)로 리스트를 만들어
		 * map.getOverlays()에 추가
		 */
		map.getOverlays().add(new SitesOverlay(marker));
		
		me = new MyLocationOverlay(this, map); //나침반이나 내 위치 표시
		map.getOverlays().add(me);
	}
	 
	private void initGeoPoint() {
		//final GeoPoint home = getPoint(  ,   );
		final GeoPoint yangyang = getPoint(36.333848,127.394629);
		final GeoPoint hyundae = getPoint(36.332768,127.390949);
		//gp.add((GeoPoint)home);
		gp.add((GeoPoint)yangyang);
		gp.add((GeoPoint)hyundae);
	}
 
	@Override
	public void onResume() {
		super.onResume();
		
		me.enableCompass(); //나침반 켜기
		//map.getController().animateTo(me.getMyLocation());
		//me.drawMyLocation(null, map, null, home, cnt);
	}		
	
	@Override
	public void onPause() {
		super.onPause(); //나침반 끄기 , 배터리 절약
		
		me.disableCompass();
	}		
	
 	@Override
	protected boolean isRouteDisplayed() {
		return(false);
	}
	
 	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_S) {   //위성모드로 전환
			map.setSatellite(!map.isSatellite());
			return(true);
		}
		else if (keyCode == KeyEvent.KEYCODE_Z) { //줌콘트롤러 불러냄
			map.displayZoomControls(true);
			return(true);
		}
		else if (keyCode == KeyEvent.KEYCODE_A) { //다음 마커위치로
			map.getController().animateTo(gp.get(cnt));
			if (++cnt == 3) cnt = 0; 
			return(true);
		}
		
		return(super.onKeyDown(keyCode, event));
	}
 
	private static GeoPoint getPoint(double lat, double lon) {
		return(new GeoPoint((int)(lat*1000000.0), (int)(lon*1000000.0)));
	}
		
	private class SitesOverlay extends ItemizedOverlay<OverlayItem> {
		private List<OverlayItem> items=new ArrayList<OverlayItem>();
		private Drawable marker=null;
		
		public SitesOverlay(Drawable marker) {
			super(marker);
			this.marker = marker;
			
			//items.add(new OverlayItem(getPoint( ,  ), "Home", "우리집"));
			items.add(new OverlayItem(getPoint(36.333848,127.394629), "YangYang", "양양 택배"));
			items.add(new OverlayItem(getPoint(36.332768,127.390949), "HyunDae", "현대 택배"));
			
			populate();
		}
		
		@Override
		protected OverlayItem createItem(int i) {
			return(items.get(i));
		}
		
		@Override
		public void draw(Canvas canvas, MapView mapView, boolean shadow) {
			super.draw(canvas, mapView, shadow);
			
			boundCenterBottom(marker);
		}
 		
		@Override //OverLay 상속받은 클래스에서 지도화면 터치하는 순간 이벤트 받음
		protected boolean onTap(int i) {
			Toast.makeText(FishingMap2.this, items.get(i).getSnippet(), Toast.LENGTH_SHORT).show();
			return(true);
		}
		
		@Override
		public int size() {
			return(items.size());
		}
	}
}